package fr.upmc.aladyn.transactionables.saveobject;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

/**
 * <p>Class used to store SaveRestoreObjects into a Stack</p>
 * @author Alexis Deluze
 *
 */
public class StackOfSaveRestoreObjects {

	private Stack<SaveRestoreObject> sros = new Stack<SaveRestoreObject>();
	private Set<Integer> index = new HashSet<Integer>();

	/**
	 * <p>Get the Stack of SaveRestoreObjects</p>
	 * @return the Stack of SaveRestoreObjects
	 */
	public Stack<SaveRestoreObject> getStack(){
		return sros;
	}
	
	/**
	 * <p>Get the index of elements stored in the Stack</p>
	 * @return the index
	 */
	public Set<Integer> getIndex(){
		return index;
	}
	
	/**
	 * <p>Add a new SaveRestoreObject to the Stack</p>
	 * @param sro the SaveRestoreObject to add
	 */
	public void add(SaveRestoreObject sro){
		if(! index.contains(sro.hashCode())){
			sros.add(sro);
		}
	}
	
	/**
	 * <p>Remove and returs the last element of the Stack</p>
	 * @return the last element of the Stack
	 */
	public SaveRestoreObject removeLast(){
		SaveRestoreObject ret = sros.pop();
		index.remove(ret.hashCode());
		return ret;
	}
}
